package anonymouschat.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;

import anonymouschat.Identity;
import anonymouschat.NickList;

/**
 * Interactions between client and local file.
 * 
 * @author hb
 * 
 */
public class LocalFileOps
{
    /**
     * Read last identity from local file.
     * 
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Identity readLastIdentity() throws IOException,
            ClassNotFoundException {
        FileInputStream inputStream = new FileInputStream(
                ConstantVariables.ID_FILE_NAME );
        ObjectInputStream ois = new ObjectInputStream( inputStream );
        Identity id = (Identity) ois.readObject();
        ois.close();
        return id;
    }

    /**
     * write new identity to identity file
     * 
     * @param newIdentity
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void updateLastIdentity( Identity newIdentity ) throws FileNotFoundException, IOException {
        ObjectOutputStream ois = new ObjectOutputStream( new FileOutputStream(
                ConstantVariables.ID_FILE_NAME ) );
        ois.writeObject( (Object) newIdentity );
        ois.close();
    }

    /**
     * remove all records associating with an obsolete identity.
     */
    public static void removeAllRecords() {
        try {
            new File( ConstantVariables.NICK_LIST_FILE_NAME ).delete();
            FileInputStream fin = null;
            fin = new FileInputStream( ConstantVariables.CHAT_USER_LIST );
            InputStreamReader isr = new InputStreamReader( fin );
            BufferedReader br = new BufferedReader( isr );
            String strLine;
            try {
                while( (strLine = br.readLine()) != null ) {
                    new File( ConstantVariables.CHAT_LOG_FILE_NAME_PREFIX
                            + strLine ).delete();
                }
            } catch( IOException e ) {
                e.printStackTrace();
            }
            new File( ConstantVariables.CHAT_USER_LIST ).delete();
        } catch( FileNotFoundException fnf ) {
        }
    }

    /**
     * Get chat history with target user.
     */
    public static String getChatLog( String userID ) {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(
                    ConstantVariables.CHAT_LOG_FILE_NAME_PREFIX + userID );
        } catch( FileNotFoundException e1 ) {
            return null;
        }
        InputStreamReader isr = new InputStreamReader( fin );
        BufferedReader br = new BufferedReader( isr );
        StringBuffer sb = new StringBuffer();
        try {
            sb.append( br.readLine() );
        } catch( IOException e ) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * Append chat history to existing log file
     * 
     * or
     * 
     * Create new chat history
     * 
     * @param userID
     * @param chatLog
     */
    public static void appendChatLog( String userID, String chatLog ) {

        FileWriter fstream = null;
        try {
            fstream = new FileWriter(
                    ConstantVariables.CHAT_LOG_FILE_NAME_PREFIX + userID, true );
        } catch( IOException e ) {
            FileOutputStream fout = null;
            try {
                fout = new FileOutputStream(
                        ConstantVariables.CHAT_LOG_FILE_NAME_PREFIX + userID );
            } catch( FileNotFoundException e1 ) {
                e1.printStackTrace();
            }
            new PrintStream( fout ).println( chatLog );
            try {
                fout.close();
            } catch( IOException e2 ) {
                e2.printStackTrace();
            }
        }
        BufferedWriter out = new BufferedWriter( fstream );
        try {
            out.write( chatLog );
        } catch( IOException e ) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch( IOException e ) {
            e.printStackTrace();
        }
    }

    /**
     * remove chat history
     * 
     * @param userID
     */
    public static void removeChatLog( String userID ) {
        new File( ConstantVariables.CHAT_LOG_FILE_NAME_PREFIX + userID )
                .delete();
    }

    /**
     * Get nick name list from file
     */
    public static NickList getNickNameList() {

        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream(
                    ConstantVariables.NICK_LIST_FILE_NAME );
        } catch( FileNotFoundException e ) {
            return null;
        }

        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream( inputStream );
        } catch( IOException e ) {
            e.printStackTrace();
        }
        NickList tempList = null;
        try {
            tempList = (NickList) ois.readObject();
        } catch( IOException e ) {
            e.printStackTrace();
        } catch( ClassNotFoundException e ) {
            e.printStackTrace();
        }
        try {
            ois.close();
        } catch( IOException e ) {
            e.printStackTrace();
        }
        return tempList;
    }

    public static void updateNicknameList( NickList nickList ) {
        ObjectOutputStream ois = null;
        try {
            ois = new ObjectOutputStream( new FileOutputStream(
                    ConstantVariables.NICK_LIST_FILE_NAME ) );
        } catch( FileNotFoundException e1 ) {
            e1.printStackTrace();
        } catch( IOException e1 ) {
            e1.printStackTrace();
        }

        try {
            ois.writeObject( (Object) nickList );
            ois.close();
        } catch( IOException e1 ) {
            e1.printStackTrace();
        }
    }
}
